<?php


namespace App\Model;


use Encore\Admin\Auth\Database\Administrator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Illuminate\Support\Facades\DB;

class Stock extends Model
{

    public const CREATED_AT = 'create_time';

    public const UPDATED_AT = null;

    public $table = "stock";

    protected $fillable = [
        'value',
        'type',
        'goods_id',
        'user_user_id'
    ];

    /**
     * 管理商品信息
     *
     * @return BelongsTo
     */
    public function good()
    {
        return $this->belongsTo(Good::class, 'goods_id');
    }

    /**
     * 管理amazon 商品
     *
     * @return HasOneThrough
     */
    public function amazonGood()
    {
        return $this->hasOneThrough(AmazonGood::class, Good::class, 'id', 'id', 'goods_id', 'amazon_goods_id');
    }

    /**
     * 管理最新的admin 信息
     * @return HasOne
     */
    public function lastAdminNote()
    {
        return $this->hasOne(Note::class, 'goods_id')
            ->select(['content', 'goods_id'])
            ->where('user_user_id', 3)
            ->orderByDesc('id');
    }

    /**
     * 管理最新的一条客服消息
     *
     * @return HasOne
     */
    public function lastCustomNote()
    {
        return $this->hasOne(Note::class, 'goods_id')
            ->select(['content', 'goods_id'])
            ->where('user_user_id', '!=', 3)
            ->orderByDesc('id');
    }

    /**
     * @desc 关联admin
     *
     *
     * @return BelongsTo
     *
     * @author: 梅杰[meijie3169@dingtalk.com] at 2020-03-10 14:12:01
     */
    public function user()
    {
        return $this->belongsTo(Administrator::class,'user_user_id');
    }



}
